package com.luodim.ruledemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.luodim.ruledemo.utils.BaseUtils;

/**
 * Created by luodi on 2017/7/10.
 */

public class MeasureView extends View {

    private float mDrawX;
    private float mDrawY;
    private Paint mPaint;
    private int mOffset = 200;
    private int mMinSlop;
    private float mLastX;
    private float mLastY;
    private Paint mPointPaint;

    public MeasureView(Context context) {
        this(context, null);
    }

    public MeasureView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MeasureView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#80000000"));
        mPaint.setStrokeWidth(BaseUtils.dp2px(getContext(), 1));
        mMinSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();

        mPointPaint = new Paint();
        mPointPaint.setAntiAlias(true);
        mPointPaint.setColor(Color.RED);
        mPointPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Log.d("edwin", "onDraw----");
        canvas.drawLine(mDrawX, 0, mDrawX, mDrawY, mPaint);
        canvas.drawLine(mDrawX, mDrawY, 0, mDrawY, mPaint);
        canvas.drawCircle(mDrawX, mDrawY, BaseUtils.dp2px(getContext(), 2), mPointPaint);
    }

    public void setCtrlPointOffset(int offset) {
        mOffset = offset;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        boolean result = false;
        int action = event.getAction();
        Log.d("edwin", "action is----" + action);
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mLastX = event.getX();
                mLastY = event.getY();
                mDrawX = mLastX - mOffset;
                mDrawY = mLastY - mOffset;
                invalidate();
                result = true;
                break;
            case MotionEvent.ACTION_MOVE:
                mDrawX = event.getX() - mOffset;
                mDrawY = event.getY() - mOffset;
                if (Math.abs(mDrawX - mLastX) > mMinSlop || Math.abs(mDrawY - mLastY) > mMinSlop) {
                    invalidate();
                }
                result = true;
                break;
            case MotionEvent.ACTION_UP:
                result = true;
                break;
        }
        return result;
    }
}
